<template>
  <div>
    <div>
      <el-card shadow="never">
        <div class="card-query">
          <div>
              <el-input v-model="search" placeholder="输入关键字搜索" />
          </div>
          <div>
            <el-button
              icon="el-icon-refresh"
              type="primary"
              @click="handleRefresh"
              >刷新</el-button
            >
          </div>
        </div>
      </el-card>

      <el-table
        :data="tableData.filter(
          (data) =>
            !search ||
            data.articleTitle.toLowerCase().includes(search.toLowerCase()) ||
            data.articleAuthor.toLowerCase().includes(search.toLowerCase()) ||
            data.adminName.toLowerCase().includes(search.toLowerCase()) 
        ) "
        border=""
        :header-cell-style="{ textAlign: 'center' }"
        :cell-style="{ textAlign: 'center' }"
      >
        <el-table-column label="Id">
          <template slot-scope="scope">
            <i class="el-icon-time"></i>
            <span>{{ scope.row.id }}</span>
          </template>
        </el-table-column>
        <el-table-column label="文章标题">
          <template slot-scope="scope">
            <span>{{ scope.row.articleTitle }}</span>
          </template>
        </el-table-column>
        <el-table-column label="作者">
          <template slot-scope="scope">
            <span>{{ scope.row.articleAuthor }}</span>
          </template>
        </el-table-column>
        <el-table-column label="文章类型">
          <template slot-scope="scope">
            <span>{{ scope.row.articleType }}</span>
          </template>
        </el-table-column>
        <el-table-column label="发表人">
          <template slot-scope="scope">
            <span>{{ scope.row.adminName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button size="mini" @click="handlePulish(scope.index, scope.row)"
              >发布</el-button
            >
            <el-popconfirm
              title="确定删除吗？"
              @confirm="handleDelete(scope.$index, scope.row)"
            >
              <el-button size="mini" type="danger" slot="reference"
                >删除</el-button
              >
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pager.pageIndex"
        :page-size="pager.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pager.rowsTotal"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getAllArticle, deleteArticle, publishArticle } from "../api/article";
export default {
  data() {
    return {
      pager: {
        pageIndex: 1,
        pageSize: 10,
        rowsTotal: 30,
      },
      //对话框的表格
      form: {},
      tableData: [],
      formLabelWidth: "120px",
      search: "",
    };
  },
  mounted() {
    this.fetchData(this.pager);
  },
  methods: {
    //编辑
    handleEdit(row) {
      localStorage.setItem("articleId", row.id);
      this.$router.push("/ArticleEditor");
    },
    handleDelete(index, row) {
      deleteArticle(row.id).then(({ data }) => {
        console.log(data);
        this.tableData.splice(index, 1);
      });
    },
    handlePulish(index, row) {
      console.log(row);
      publishArticle(row.id).then(({ data }) => {
        console.log(data);
        this.tableData.splice(index, 1);
      });
    },
    //查找
    handleQuery() {},
    //刷新
    handleRefresh() {
      location.reload();
    },
    //分页
    handleSizeChange(val) {
      console.log(val);
      this.pager.pageSize = val;
      this.fetchData(this.pager);
    },
    handleCurrentChange(val) {
      console.log(val);
      this.pager.pageIndex = val;
      this.fetchData(this.pager);
    },
    fetchData(pager) {
      console.log(pager);
      getAllArticle(pager).then(({ data }) => {
        this.tableData = data.data.data;
        this.pager = data.data.pager;
        console.log(data.data.data);
      });
    },
  },
};
</script>

<style scoped>
.card-query,
.form-query {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.btnQuery {
  margin-left: 10px;
}
.form-query .el-form-item {
  margin-bottom: 0px;
}
.block {
  text-align: center;
}
</style>